﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDSimpleStatus.Models;

namespace BDSimpleStatus.Controllers
{
    [RequiresAuthentication]
    public class SettingsController : Controller
    {
        //
        // GET: /Settings/

        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public ActionResult Index()
        {
            BdssRepository repo = new BdssRepository();
            List<ServerCheckModel> scml = repo.GetServerCheckModelList();
            return View(scml);
        }

        //
        // GET: /Settings/Details/5

        public ActionResult Details(int id)
        {
            BdssRepository repo = new BdssRepository();
            ServerCheckModel scm = repo.GetServerCheckModel(id);
            return View(scm);
        }

        //
        // GET: /Settings/Create

        public ActionResult Create()
        {
            ServerCheckModel scm = new ServerCheckModel();
            return View(scm);
        } 

        //
        // POST: /Settings/Create

        [HttpPost]
        public ActionResult Create(ServerCheckModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    BdssRepository repo = new BdssRepository();
                    repo.AddServerCheck(model);
                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    logger.ErrorException("Settings/Create new check", ex);
                    return View();
                }
            }
            else
            {
                logger.Warn("Settings/Create Model State came back invalid from create action");
                return RedirectToAction("Create");
            }
        }
        
        //
        // GET: /Settings/Edit/5
 
        public ActionResult Edit(int id)
        {
            BdssRepository repo = new BdssRepository();
            ServerCheckModel scm = repo.GetServerCheckModel(id);
            if (scm.Id == 0 || string.IsNullOrEmpty(scm.URL))
            {
                return RedirectToAction("Index");
            }
            else 
                return View(scm);
        }

        //
        // POST: /Settings/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ServerCheckModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add update logic here
                    BdssRepository repo = new BdssRepository();
                    repo.UpdateServerCheck(model, true);
                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    logger.ErrorException("Settings/Edit error", ex);
                    return View();
                }
            }
            logger.Warn("Settings/Edit Model State came back invalid from create action");
            return RedirectToAction("Index");
        }

        //
        // GET: /Settings/Delete/5
 
        public ActionResult Delete(int id)
        {
            BdssRepository repo = new BdssRepository();
            ServerCheckModel scm = repo.GetServerCheckModel(id);
            if (scm.Id == 0 || string.IsNullOrEmpty(scm.URL))
            {
                return RedirectToAction("Index");
            }
            else 
             return View(scm);
        }

        //
        // POST: /Settings/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    BdssRepository repo = new BdssRepository();
                    ServerCheckModel scm = repo.GetServerCheckModel(id);
                    if (scm.Id == 0 || string.IsNullOrEmpty(scm.URL))
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        repo.DeleteServerCheck(scm);
                        return RedirectToAction("Index");
                    }
                }
                catch
                {
                    return View();
                }
            }
            else
            {
                logger.Warn("Settings/Delete Model State came back invalid from create action");
                return View();
            }
        }
    }
}
